package com.shareyi.simpleserver.handler;

import java.io.IOException;
import java.net.Socket;

import com.shareyi.simpleserver.core.HttpRequest;
import com.shareyi.simpleserver.core.HttpResponse;
import com.shareyi.simpleserver.core.SimpleHttpServer;
import com.shareyi.simpleserver.servlet.Servlet;
import com.shareyi.simpleserver.servlet.ServletException;
import com.shareyi.simpleserver.servlet.WebApplication;

public class ServletHandler implements Handler {

	private static ServletHandler servletHandler;
	private WebApplication webApplication;
	
	public void doHandle(Socket connection, HttpRequest request,
			HttpResponse response)  throws IOException, ServletException{
		this.webApplication=SimpleHttpServer.getServerInstance().getWebApplication();
		Servlet servlet=webApplication.getServletByUrl(request.getUrl());
		servlet.service(request, response);
	}

	public static Handler getSingleHandler() {
		if(servletHandler==null){
			servletHandler=new ServletHandler();
		}
		return servletHandler;
	}

	public void setWebApplication(WebApplication webApplication) {
		this.webApplication=webApplication;
	}


}
